package com.car.rental.gateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.event.EventListener;
import org.springframework.cloud.gateway.route.RouteDefinitionLocator;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    
    private static final Logger log = LoggerFactory.getLogger(GatewayApplication.class);
    
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
    
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("auth-service", r -> r.path("/auth/**")
                        .uri("lb://car-rental-auth"))
                .build();
    }
    
    @EventListener
    public void logRoutes(ApplicationReadyEvent event) {
        RouteDefinitionLocator routeDefinitionLocator = event.getApplicationContext().getBean(RouteDefinitionLocator.class);
        routeDefinitionLocator.getRouteDefinitions()
                .subscribe(routeDefinition -> {
                    log.info("Route ID: {}", routeDefinition.getId());
                    log.info("Route URI: {}", routeDefinition.getUri());
                    log.info("Route Predicates: {}", routeDefinition.getPredicates());
                    log.info("Route Filters: {}", routeDefinition.getFilters());
                    log.info("------------------------");
                });
    }
} 